package com.bdqn.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
/**
 * 系统管理
 * @author derongrong
 *
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bdqn.dto.Msg;
import com.bdqn.model.CaiwuList;
import com.bdqn.model.CustomerBaseInfo;
import com.bdqn.model.IndividualAccount;
import com.bdqn.model.TransactionType;
import com.bdqn.model.Users;
import com.bdqn.pojo.Temporary;
import com.bdqn.service.FinanceService;
import com.bdqn.service.TransactionService;
import com.bdqn.service.TransactionTypeService;
@RequestMapping("/Finance")
@Controller
public class FinanceController {
	//用户表
	@Autowired
	private FinanceService financeService;
	
	//财务类型表
	@Autowired
	private TransactionTypeService transactiontypeService;
	
	//交易表
	@Autowired
	private TransactionService transactionService;
	
	
	//查找财务类型
	@RequestMapping("/select")
	public String seekType(HttpServletRequest request) {
		List<CaiwuList> transactionType=transactiontypeService.seekType();
		if(transactionType != null) {
			request.getSession().setAttribute("transactionType", transactionType);
		}
		return "finance";
	}
	
	//查询用户名
	@RequestMapping("/result/{message}")
	@ResponseBody
	public List<CustomerBaseInfo> usersData(@PathVariable("message")String name){
		List<CustomerBaseInfo> user=financeService.selectUser(name);
		return user;
		
	}

	//插入数据
	@RequestMapping("/transfer/{userName}/{denglumingzi}")
	@ResponseBody
	@Transactional(rollbackFor = Exception.class)
	public Msg insertData(@PathVariable("userName")String userName,@PathVariable("denglumingzi")String loginName,Temporary temporary ) {
		boolean idExits=financeService.idExits(userName);
		Msg msg=new Msg();
		//账户明细
		IndividualAccount individualAccount =new IndividualAccount();
		if(!idExits) {
			msg.setCode(500);
			msg.setName("您的账户不存在!");
		}else {
			boolean u=financeService.state(userName);
			if(!u) { //查到有就进行进去捉错
				msg.setCode(403);
				msg.setName("你的账户没有启用状态!");
			}else {
				//查找余额
				IndividualAccount jinqian=financeService.queryBalance(userName);
				Float balance=jinqian.getBalance(); //总金额
				System.out.println(balance);
				//截取金额字符
				String cutOut= temporary.getMonery().substring(1,temporary.getMonery().length());
				String arithmetic= temporary.getMonery().substring(0,1); //截取字符
				Float money=Float.parseFloat(cutOut);	//转换类型
				System.out.println(individualAccount.getCustomId());
				System.out.println(money);
				System.out.println(arithmetic);
				if( money > balance && arithmetic.equals("-")) {  //账户余额不足
					System.out.println("您的余额不足!");
					msg.setCode(505);
					msg.setName("您的余额不足,扣费失败!");
				}else if( money <= balance && arithmetic.equals("-")){   //可以进行转账
					float residueMoney=balance-money;
					//账户变动
					boolean updateMoney=transactionService.updateMoney(jinqian.getCustomId(),residueMoney);
					boolean flag=transactionService.residueMoney(loginName, residueMoney ,temporary);
					if(!flag  && !updateMoney) {
						msg.setCode(3);
						msg.setName("扣费失败!");
					}else {
						System.out.println("剩余金额: "+residueMoney);
						msg.setCode(200);
						msg.setName("扣费成功!");
					}
				}else if(arithmetic.equals("+")) {
					float residueMoney=balance+money;
					//账户变动
					boolean updateMoney=transactionService.updateMoney(jinqian.getCustomId(),residueMoney);
					boolean flag=transactionService.residueMoney(loginName, residueMoney ,temporary);
					if(!flag  && !updateMoney) {
						msg.setCode(3);
						msg.setName("扣费失败!");
					}else {
						System.out.println("剩余金额: "+residueMoney);
						msg.setCode(200);
						msg.setName("扣费成功!");
					}
				}
			}
		}
		
		return msg;
	}
	
}
